﻿using System.Reflection;

namespace AbpCodeGenerater.Core
{
    public class AbpDomainClass : AbpClass
    {
        public Type ClassType { get; set; }

        public Type? SharedClassType { get; set; }

        public AbpProject? DomainProject { get; set; }

        public AbpProject? DomainSharedProject { get; set; }

        public AbpProject? EntityFrameworkCoreProject { get; set; }

        public AbpProject? ApplicationProject { get; set; }

        public AbpProject? ApplicationContractsProject { get; set; }

        public AbpProject? HttpApiProject { get; set; }

        public AbpDomainClass(AbpClass abpClass, Type type)
        {
            Name = abpClass.Name;
            FileName = abpClass.FileName;
            FullPath = abpClass.FullPath;
            AbpProject = abpClass.AbpProject;
            Context = abpClass.Context;
            ClassSummary = abpClass.ClassSummary;
            PropertySummaries = abpClass.PropertySummaries;
            if (PropertySummaries != null)
            {
                if (!PropertySummaries.ContainsKey("Code")) PropertySummaries.Add("Code", "编号");
                if (!PropertySummaries.ContainsKey("SourceType")) PropertySummaries.Add("SourceType", "来源类型");
                if (!PropertySummaries.ContainsKey("SourceKey")) PropertySummaries.Add("SourceKey", "来源主键");
                if (!PropertySummaries.ContainsKey("Children")) PropertySummaries.Add("Children", "子项集合");
                if (!PropertySummaries.ContainsKey("MainArchiveId")) PropertySummaries.Add("MainArchiveId", "主档案");
            }

            DomainProject = abpClass.AbpProject;
            if (Directory.Exists(DomainProject?.FullPath + ".Shared"))
            {
                DomainSharedProject = new AbpProject
                {
                    Name = DomainProject?.Name + ".Shared",
                    Path = DomainProject?.Path + ".Shared",
                    FullPath = DomainProject?.FullPath + ".Shared",
                    AbpSolution = DomainProject?.AbpSolution
                };

                var sharedDllPath = Path.Combine(
                    abpClass.AbpProject?.AbpSolution?.DirectioryPath ?? string.Empty,
                    abpClass.AbpProject?.AbpSolution?.StartUpProject?.Path ?? string.Empty,
                    "bin",
                    "Debug",
                    "net6.0",
                    $"{DomainSharedProject.Name}.dll"
                    );
                var assembly = Assembly.LoadFrom(sharedDllPath);
                SharedClassType = assembly.GetTypes().FirstOrDefault(x => x.Name == $"{abpClass.Name}Consts");
            }

            if (Directory.Exists(DomainProject?.FullPath?.Replace(".Domain", ".EntityFrameworkCore")))
            {
                EntityFrameworkCoreProject = new AbpProject
                {
                    Name = DomainProject?.Name?.Replace(".Domain", ".EntityFrameworkCore"),
                    Path = DomainProject?.Path?.Replace(".Domain", ".EntityFrameworkCore"),
                    FullPath = DomainProject?.FullPath?.Replace(".Domain", ".EntityFrameworkCore"),
                    AbpSolution = DomainProject?.AbpSolution
                };
            }

            if (Directory.Exists(DomainProject?.FullPath?.Replace(".Domain", ".Application")))
            {
                ApplicationProject = new AbpProject
                {
                    Name = DomainProject?.Name?.Replace(".Domain", ".Application"),
                    Path = DomainProject?.Path?.Replace(".Domain", ".Application"),
                    FullPath = DomainProject?.FullPath?.Replace(".Domain", ".Application"),
                    AbpSolution = DomainProject?.AbpSolution
                };
            }

            if (Directory.Exists(DomainProject?.FullPath?.Replace(".Domain", ".Application.Contracts")))
            {
                ApplicationContractsProject = new AbpProject
                {
                    Name = DomainProject?.Name?.Replace(".Domain", ".Application.Contracts"),
                    Path = DomainProject?.Path?.Replace(".Domain", ".Application.Contracts"),
                    FullPath = DomainProject?.FullPath?.Replace(".Domain", ".Application.Contracts"),
                    AbpSolution = DomainProject?.AbpSolution
                };
            }

            if (Directory.Exists(DomainProject?.FullPath?.Replace(".Domain", ".HttpApi")))
            {
                HttpApiProject = new AbpProject
                {
                    Name = DomainProject?.Name?.Replace(".Domain", ".HttpApi"),
                    Path = DomainProject?.Path?.Replace(".Domain", ".HttpApi"),
                    FullPath = DomainProject?.FullPath?.Replace(".Domain", ".HttpApi"),
                    AbpSolution = DomainProject?.AbpSolution
                };
            }

            ClassType = type;
        }
    }
}
